package chexuan.game.interfaces.io.connection;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;

import chexuan.game.bean.packet.common.Packet;

public class PacketCollector {

	private final PacketFilter packetFilter;
	private final ArrayBlockingQueue<Packet> resultQueue;
	private final AbstractSocketConnection connection;
	private boolean cancelled = false;

	/**
	 * Creates a new packet collector. If the packet filter is <tt>null</tt>,
	 * then all packets will match this collector.
	 * 
	 * @param conection
	 *            the connection the collector is tied to.
	 * @param packetFilter
	 *            determines which packets will be returned by this collector.
	 */
	protected PacketCollector(AbstractSocketConnection conection, PacketFilter packetFilter) {
		this(conection, packetFilter, 1000);
	}

	/**
	 * Creates a new packet collector. If the packet filter is <tt>null</tt>,
	 * then all packets will match this collector.
	 * 
	 * @param conection
	 *            the connection the collector is tied to.
	 * @param packetFilter
	 *            determines which packets will be returned by this collector.
	 * @param maxSize
	 *            the maximum number of packets that will be stored in the
	 *            collector.
	 */
	protected PacketCollector(AbstractSocketConnection conection, PacketFilter packetFilter,
			int maxSize) {
		this.connection = conection;
		this.packetFilter = packetFilter;
		this.resultQueue = new ArrayBlockingQueue<Packet>(maxSize);
	}

	/**
	 * Explicitly cancels the packet collector so that no more results are
	 * queued up. Once a packet collector has been cancelled, it cannot be
	 * re-enabled. Instead, a new packet collector must be created.
	 */
	public void cancel() {
		// If the packet collector has already been cancelled, do nothing.
		if (!cancelled) {
			cancelled = true;
			connection.removePacketCollector(this);
		}
	}

	/**
	 * Returns the packet filter associated with this packet collector. The
	 * packet filter is used to determine what packets are queued as results.
	 * 
	 * @return the packet filter.
	 */
	public PacketFilter getPacketFilter() {
		return packetFilter;
	}

	/**
	 * Polls to see if a packet is currently available and returns it, or
	 * immediately returns <tt>null</tt> if no packets are currently in the
	 * result queue.
	 * 
	 * @return the next packet result, or <tt>null</tt> if there are no more
	 *         results.
	 */
	public Packet pollResult() {
		return resultQueue.poll();
	}

	/**
	 * Returns the next available packet. The method call will block (not
	 * return) until a packet is available.
	 * 
	 * @return the next available packet.
	 */
	public Packet nextResult() {
		try {
			return resultQueue.take();
		} catch (InterruptedException e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * Returns the next available packet. The method call will block (not
	 * return) until a packet is available or the <tt>timeout</tt> has elapased.
	 * If the timeout elapses without a result, <tt>null</tt> will be returned.
	 * 
	 * @param timeout
	 *            the amount of time to wait for the next packet (in
	 *            milleseconds).
	 * @return the next available packet.
	 */
	public Packet nextResult(long timeout) {
		try {
			return resultQueue.poll(timeout, TimeUnit.MILLISECONDS);
		} catch (InterruptedException e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * Processes a packet to see if it meets the criteria for this packet
	 * collector. If so, the packet is added to the result queue.
	 * 
	 * @param packet
	 *            the packet to process.
	 */
	protected void processPacket(Packet packet) {
		if (packet == null) {
			return;
		}

		if (packetFilter == null || packetFilter.accept(packet)) {
			while (!resultQueue.offer(packet)) {
				// Since we know the queue is full, this poll should never
				// actually block.
				resultQueue.poll();
			}
		}
	}
}
